<?php
include_once '../Login/databaseconnection.php';
$Pro_Id=$_POST['Pro_Id'];
$New_Programme=$_POST['New_Programme'];
if($New_Programme!=""&&$Pro_Id!="Select One")
{
$_SESSION['error']="Please select the programme or type new!";
}
else
{
   date_default_timezone_set("Europe/Paris");
   $t_d=date("Y-m-d");
if($New_Programme!="")
{
$maxid=mysql_query("SELECT max(Programme_Id) as maxId FROM programme");

while($rowid=mysql_fetch_array($maxid))
{
$m_Id=$rowid['maxId'];
$inum=substr("$m_Id",1);
$inn=($inum+1);
$Programme_Id='P';
if(strlen($inn)<strlen($inum))
{
for($i=0;$i<(strlen($inum)-strlen($inn));$i++)
{
$Programme_Id=$Programme_Id.'0';
}
$Programme_Id=$Programme_Id.$inn;
}
else
{
$Programme_Id=$Programme_Id.$inn;
}
}
$sql_pro=mysql_query("INSERT INTO programme(Programme_Id,Programme_Name,Last_Updated_Date)values('$Programme_Id','$New_Programme','$t_d')");
if($sql_pro)
{
$Pro_Id=$Programme_Id;
}
}
$Required_Teachers=$_POST['Required_Teachers'];
$Grade=$_POST['Grade'];
$Year=$_POST['Year'];
$No_M_Stu=$_POST['No_M_Stu'];
$No_F_Stu=$_POST['No_F_Stu'];
$No_Tra_tea=$_POST['No_Tera_tea'];
$Tra_tea=$_POST['Tra_tea'];
$Avai_Book=$_POST['Avai_Book'];
$Avai_Tea_Gui=$_POST['Avai_Tea_Gui'];
   	$SchName=$_POST['SchName'];
	$name=explode(",",$SchName);
    $nam=$name[0];
$tn=count($name);
for($i=1;$i<$tn;$i++)
{
if(empty($adr))
{
$adr=$name[$i];
}
else
{
$adr=$adr.",".$name[$i];
}
}
$results= mysql_query("SELECT * FROM school Where Name='$nam' AND Address='$adr'");
if($row = mysql_fetch_array($results))
{
 $Sch_Id=$row['School_Id'];
}
  if(($Pro_Id!="Select One"||$New_Programme!="")&&$Year!=""&&$SchName!=""&&$No_M_Stu!=""&&$No_F_Stu!=""&&$No_Tra_tea!=""&&$Tra_tea!=""&&$Required_Teachers!=""&&$Avai_Book!=""&&$Avai_Tea_Gui!=""&&$Grade!="")
{
$sql=mysql_query("INSERT INTO programmes(Grade,Required_Teachers,Programme_Id,Year,School_Id,No_Of_Male_Students,No_Of_Female_Students,Teachers_In_Training,Trained_Teachers,Availability_Books,Availability_Tguides,Last_Updated_Date)values('$Grade','$Required_Teachers','$Pro_Id','$Year','$Sch_Id','$No_M_Stu','$No_F_Stu','$No_Tra_tea','$Tra_tea','$Avai_Book','$Avai_Tea_Gui','$t_d')");
if($sql)
  {
$_SESSION['error']="This record is successfully registered!";
$_SESSION['OK']="OK";
$_SESSION['Pro_Id']=$Pro_Id;
$_SESSION['Year']=$Year;
$_SESSION['Sch_Id']=$Sch_Id; 
$_SESSION['Grade']=$Grade; 
  }

  if(mysql_affected_rows()==0)
  {
$_SESSION['error']="Sorry unable to register this record!";
  }
  }
 if(($Pro_Id=="Select One"&&$New_Programme=="")||$Year==""||$SchName==""||$No_M_Stu==""||$No_F_Stu==""||$No_Tra_tea==""||$Tra_tea==""||$Required_Teachers==""||$Avai_Book==""||$Avai_Tea_Gui=="")
	{
	$_SESSION['errorin']="Please fill all!";
  }
}
?>
